<?php


namespace App\Admin\Actions\Tools;


use App\Admin\Actions\BatchAction;
use App\Agency;
use App\Models\AdminUser;
use App\PatentApply;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;

class AssignAgencyBatchAction extends BatchAction
{
    protected $selector = '.assign-agency-posts';

    public function handle(Collection $collection, Request $request)
    {
        $id = $request->get('id', 0);
        foreach ($collection as $model) {
            if ($model instanceof PatentApply) {
                $model->agency_id = $id;
                $model->save();
            }
        }
        return $this->response()->success('操作成功！')->refresh();
    }


    public function form()
    {
        $this->select('id', '请选择意向代理机构')->options(Agency::selectOptions())->required();
    }

    public function html()
    {
        return "<a class='assign-agency-posts btn btn-sm btn-primary'>指派代理机构</a>";
    }

    public function authorize(AdminUser $user, Collection $collection)
    {
        return $user->role_type == 1;
    }

}
